﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web.UI;
using AcctrueWMS.Foundation.WebControls;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using AcctrueWMS.Foundation.Web;
using AcctrueWMS.Foundation.Web.Permission;
using AcctrueWMS.Foundation.Web.Session;

namespace AcctrueWMS.Web
{
    /// <summary>
    /// 左侧菜单页面
    /// </summary>
    public partial class Left : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string RenderTree()
        {
            StringBuilder sb = new StringBuilder(@"<table cellspacing='0' cellpadding='0' style='width: 200px; height:100%; background: #ffffff; margin-left: 5px;'><tr><td style='background: url(res/images/leftbj_01.jpg) repeat-y left;height:12px;'> </td> </tr><tr><td style='height: 100%; vertical-align: top; background-image: url(res/images/leftbj_02.jpg); background-attachment: scroll; background-repeat: repeat-y; background-position-x: left; background-position-y: 50%; background-color: transparent;'> <div id='divMenu' style='width:98%'><table class='tblMenu'>");
            List<Module> dtSource = getSysModule();
            Dictionary<Module, List<Module>> moduleTree = new Dictionary<Module, List<Module>>();
            for (int i = 0; i < dtSource.Count; i++)
            {
                if (dtSource[i].Parent == "0" && CopareModulePermission(dtSource[i]))
                {
                    List<Module> temp = new List<Module>();
                    for (int j = 0; j < dtSource.Count; j++)
                    {
                        if (dtSource[j].Parent!="0"&&dtSource[j].Parent == dtSource[i].Name && CopareModulePermission(dtSource[j]))
                        {
                            temp.Add(dtSource[j]);
                            dtSource.Remove(dtSource[j]);
                            j--;
                        }
                    }
                    moduleTree.Add(dtSource[i], temp);
                    dtSource.Remove(dtSource[i]);
                    i--;
                }
            }
            int tempInt = 1;
            foreach (KeyValuePair<Module, List<Module>> item in moduleTree)
            {
                sb.AppendFormat("<tr style=\"vertical-align: top;\"><td id='menuTD{0}' style='height:auto; vertical-align: top; '>", tempInt);
                sb.AppendFormat("<p onclick='ShowMenu({0})'><a class='submenu{0}'>{1}</a></p>", tempInt, item.Key.Text);

                if (tempInt == 1)
                {
                    sb.AppendFormat("<ul id='submenu{0}' class='submenu' style='display:block;'>", tempInt);
                }
                else
                {
                    sb.AppendFormat("<ul id='submenu{0}' class='submenu' style='display:none;'>", tempInt);
                }
                foreach(Module childs in item.Value)
                {
                    sb.AppendFormat("<li><a href='#' onclick=\"select('{0}',this);showWaiting();\" >{1}</a></li>", childs.Url,childs.Text);
                }
                sb.Append("</ul>");
                sb.Append("</td></tr>");
                tempInt++;

            }
            sb.AppendFormat("</table></div>  </td> </tr> <tr><td style=\"margin:0;padding:0;border:0;\"> <img alt='' src='res/images/leftbj_03.jpg' /></td></tr></table> <input type='hidden' name='hfMenuCount' id='hfMenuCount' value='{0}' />", moduleTree.Count);
            return sb.ToString();
        }

        //查找用户节点权限
        public bool CopareModulePermission(Module checkModule)
        {
            bool inModule = false;
            if (System.Web.HttpContext.Current.Session[SessionConst.UserName].ToString() == SessionConst.admin)
            {
                inModule = true;
            }
            else
            {
                ModuleAuthorityManager moduleAuthority = new ModuleAuthorityManager();
                List<RoleModule> userModules = moduleAuthority.GetCurrentUserModules();
                foreach (RoleModule item in userModules)
                {
                    if (item.ModuleWBS == checkModule.Name)
                    {
                        inModule = true;
                    }
                }
            }

            return inModule;
        }

        /// <summary>
        /// 获取系统module.xml 信息 
        /// </summary>
        /// <returns></returns>
        public List<Module> getSysModule()
        {
            List<Module> userModules = new List<Module>();
            XmlDocument leftDirectory = new XmlDocument();
            leftDirectory.Load(Runtime.SysFilePath + "\\Startup\\Modules.xml");
            foreach (XmlNode node in leftDirectory.SelectNodes("modules/child/module"))
            {
                if (bool.Parse(node.Attributes["showInMenu"].Value))
                {
                    Module newModule = new Module();
                    newModule.Parent = node.Attributes["parent"].Value;
                    newModule.Name = node.Attributes["name"].Value;
                    newModule.Text = node.Attributes["text"].Value;
                    newModule.Url = node.Attributes["url"].Value;
                    userModules.Add(newModule);

                }
            }
            foreach (XmlNode node in leftDirectory.SelectNodes("modules/child/module/child/module"))
            {
                if (bool.Parse(node.Attributes["showInMenu"].Value))
                {
                    Module newModule = new Module();
                    newModule.Parent = node.Attributes["parent"].Value;
                    newModule.Name = node.Attributes["name"].Value;
                    newModule.Text = node.Attributes["text"].Value;
                    newModule.Url = node.Attributes["url"].Value;
                    userModules.Add(newModule);
                }
            }
            return userModules;
        }



        private void FillTableWithXml(XmlNode root, DataTable dt)
        {
            DataRow row = dt.NewRow();
            row["key"] = root.Attributes["name"].Value;
            row["name"] = root.Attributes["text"].Value;
            row["value"] = root.Attributes["url"].Value;
            row["text"] = root.Attributes["text"].Value;
            row["wbsCode"] = root.Attributes["wbsCode"].Value;
            dt.Rows.Add(row);
            foreach (XmlNode child in root.SelectNodes("child/module"))
            {
                FillTableWithXml(child, dt);
            }
        }

        private XDocument GetXmlObject()
        {
            string fileFullPath = Server.MapPath("~") + "SysFiles\\Startup\\Modules.xml";
            XDocument xdoc = XDocument.Load(fileFullPath);
            return xdoc;
        }
    }

    public class Module
    {
        public string Name
        {
            get;
            set;
        }
        public string Key
        {
            get;
            set;
        }
        public string value
        {
            get;
            set;
        }
        public string Text
        {
            get;
            set;
        }
        public string WbsCode
        {
            get;
            set;
        }
        public string Parent
        {
            get;
            set;
        }
        public string Url
        {
            get;
            set;
        }
    }

}